package com.yunhe.spring.struts.controller;

import java.util.HashMap;
import java.util.Map;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.yunhe.spring.mybatis.model.Goods;
import com.yunhe.spring.mybatis.model.Pages;
import com.yunhe.spring.mybatis.service.impl.GoodsServiceImpl;

public class GoodsAction extends ActionSupport implements ModelDriven<Object> {

	/**
	 * GoodsController
	 */
	private static final long serialVersionUID = 1L;

	@Autowired
	private GoodsServiceImpl goodsService;
	private Goods goods;
	private Pages pages;
	private Integer curPageNum;
	private Integer pageSize;

	public GoodsServiceImpl getGoodsService() {
		return goodsService;
	}

	public void setGoodsService(GoodsServiceImpl goodsService) {
		this.goodsService = goodsService;
	}

	public Goods getGoods() {
		return goods;
	}

	public void setGoods(Goods goods) {
		this.goods = goods;
	}

	public Pages getPages() {
		return pages;
	}

	public void setPages(Pages pages) {
		this.pages = pages;
	}

	public Integer getCurPageNum() {
		return curPageNum;
	}

	public void setCurPageNum(Integer curPageNum) {
		this.curPageNum = curPageNum;
	}

	public Integer getPageSize() {
		return pageSize;
	}

	public void setPageSize(Integer pageSize) {
		this.pageSize = pageSize;
	}

	@Override
	public Object getModel() {
		if (goods == null) {
			goods = new Goods();
		}
		return goods;
	}

	// 根据前台传过来的页面大小，当前页，模糊商品名字查询出商品信息放入Pages对象,处理相应业务
	public String getAllGoods(){
		Map<String, Object> map=new HashMap<String,Object>();
		map.put("curPageNum", curPageNum);
		map.put("pageSize", pageSize);
		map.put("goods", goods);
		pages=goodsService.getAllGoods(map);
		return SUCCESS;
	}
	
		// 添加商品
		public String addGoods() {
			goodsService.addGoods(goods);
			return SUCCESS;
		}

		// 删除商品
		public String delGoods() {
			goodsService.delGoods(goods);
			return SUCCESS;
		}

		// 查找单个商品--用作修改商品
		public String getGoodsOne() {
			goods=goodsService.getGoodsOne(goods);
			ServletActionContext.getRequest().setAttribute("goods", goods);
			return SUCCESS;
		}

		// 修改对应商品
		public String editGoods() {
			goodsService.editGoods(goods);
			return SUCCESS;
		}
}
